#!/usr/bin/env python
#
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# The Original Code is Copyright (C) 2006, Blender Foundation
# All rights reserved.
#
# The Original Code is: all of this file.
#
# Contributor(s): Nathan Letwory.
#
# ***** END GPL LICENSE BLOCK *****

import sys
import os

def normpath(path):
    return os.path.abspath(os.path.normpath(path))

Import ('env')
cflags = ''
defines = []
root_build_dir=normpath(env['BF_BUILDDIR'])

source_files = ['makesdna.c']
header_files = env.Glob('../*.h')

makesdna_tool = env.Clone()
dna = env.Clone()
makesdna_tool.Append(CCFLAGS = '-DBASE_HEADER="\\"source/blender/makesdna/\\"" ')

makesdna_tool.Append (CPPPATH = ['#/intern/guardedalloc',
                                 '#/intern/atomic',
                                 '../../makesdna', '../../bmesh',
                                 '../../blenlib'])

if env['OURPLATFORM'] == 'linuxcross':
    USE_WINE = True # when cross compiling on linux 64bit this is useful
else:
    USE_WINE = False

if not USE_WINE:
    if env['OURPLATFORM'] == 'linuxcross':
        makesdna_tool.Replace(CC='gcc')
        makesdna_tool.Replace(AR='ar')
        makesdna_tool.Replace(LINK='gcc')

if sys.platform != 'cygwin':
    makesdna_tool.Append (CCFLAGS = cflags)
makesdna_tool.Append (CPPDEFINES = defines)
makesdna_tool.Append( CFLAGS = env['CFLAGS'])
makesdna_tool.Append( CCFLAGS = env['CCFLAGS'])
makesdna_tool.Append( LINKFLAGS = env['PLATFORM_LINKFLAGS'])
targetdir = normpath(root_build_dir+'/lib')

if not (root_build_dir[0]==os.sep or root_build_dir[1]==':'):
    targetdir = '#'+targetdir
makesdna_tool.Append (LIBPATH = targetdir)
if env['BF_PROFILE']:
    makesdna_tool.Append (LINKFLAGS = env['BF_PROFILE_LINKFLAGS'])

targetdir = normpath(root_build_dir + '/makesdna')

if not (root_build_dir[0]==os.sep or root_build_dir[1]==':'):
    targetdir = '#' + targetdir

makesdna = makesdna_tool.Program (target = targetdir, source = source_files, LIBS=['bf_intern_guardedalloc', 'bf_blenlib'])

dna_dict = dna.Dictionary()
dna.Depends ('dna.c', makesdna)
dna.Depends ('dna.c', header_files)

if env['OURPLATFORM'] != 'linuxcross':
    dna.Command ('dna.c', '', "\"" + root_build_dir+os.sep+"makesdna\" $TARGET")
else:
    dna.Command ('dna.c', '', ('', 'wine ')[USE_WINE] + root_build_dir+os.sep+"makesdna $TARGET")

# TODO, get WITH_DNA_GHASH working, see CMake's 'WITH_DNA_GHASH'
obj = ['intern/dna.c', 'intern/dna_genfile.c']
Return ('obj')
